<!--
 * @FilePath: /fxfzywpt/src/views/uncertain/lossAssessment1.vue
 * @Description: 
-->
<template>
  <div class="app-container">
    <!-- 公共顶部 -->
    <large-screen-header />
    <!-- 主体部分 -->
    <div class="app-main">
      <!-- 左侧 -->
      <div class="app-left">
        <div class="app-left-total">
          全国设定地震个数
          <span>
            <i v-for="item in totalArr" :key="item">{{ item }}</i>
          </span>
        </div>
        <div class="app-left-item">
          <!-- 次级标题 -->
          <!-- <subheadings :name="'各省（直辖市）设定地震个数统计'" /> -->
          <!-- <CollapsibleTable :name="'各省（直辖市）设定地震个数统计'" /> -->
        </div>
        <div class="app-left-item">
          <subheadings :name="'自2015年以来破坏性地震验证情况统计'" />
          <earthquake />
        </div>
        <div class="app-left-item">
          <subheadings :name="'设定地震个数增长趋势图'" />
          <ColumnLineChart />
        </div>
      </div>
      <!-- 中心部分 -->
      <div class="app-center">
        <!-- 标题 -->
        <headline :name="'全国设定地震灾害损失分布图'" />
        <!-- 地图 -->
        <div class="app-map">
          <!-- <IndexMap></IndexMap> -->
          <OlMap :refId="'disasterLosses'" style="height: 100%" @getMap="getMap" @mapClick="mapClick"></OlMap>
          <div ref="popupElement" class="popup-bg" id="map-popup">
            <div class="popup-head">
              <div class="popup-head-title">XXX县历年来地震灾损情况表</div>
              <div class="popup-head-x">X</div>
            </div>
            <div class="popup-content">
              <div class="popup-table">
                <el-table max-height="150" :data="tableData">
                  <el-table-column prop="time" label="发震时间" :header-align="headerAlign">
                    <template #default="scope">
                      <div class="centered-cell">{{ scope.row.time }}</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="level" label="震级" :header-align="headerAlign">
                    <template #default="scope">
                      <div style="color: #46ffff" class="centered-cell">{{ scope.row.level }}</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="lost" label="损失值" :header-align="headerAlign">
                    <template #default="scope">
                      <div style="color: #46ffff" class="centered-cell">{{ scope.row.lost }}</div>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 右侧 -->
      <div class="app-right">
        <div class="app-right-item">
          <subheadings :name="'设定地震灾害损失评估成果'" />
          <LossAssessment />
        </div>
        <div class="app-right-item">
          <subheadings :name="'现场抽样复核情况'" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="Index">
import earthquake from '@/views/disasterLosses/components/earthquake.vue';
import ColumnLineChart from '@/views/disasterLosses/components/ColumnLineChart.vue';
// import CollapsibleTable from '@/views/disasterLosses/components/CollapsibleTable.vue';
import LossAssessment from '@/views/disasterLosses/components/LossAssessment.vue';
import OlMap from '@/components/Olmap';
import { Style, Circle as CircleStyle, Fill, Stroke, Icon } from 'ol/style';
import { Feature } from 'ol';
import { Point } from 'ol/geom';
import { Vector as VectorLayer } from 'ol/layer';
import { Vector as VectorSource } from 'ol/source';
// start
import yellowEllipse from '@/assets/map/image/newImage/ellipse487.png';
import Overlay from 'ol/Overlay';
// end

// 地震个数统计
let totalNum = '1024';
const totalArr = ref([]);

totalArr.value = Array.from(totalNum);
// start
const tableData = [];
for (let i = 0; i < 10; i++) {
  tableData.push({
    level: i + 1,
    lost: +i,
    time: '2021-08-08',
  });
}
const mapInstance = ref(null);
const vectorLayer = ref(null);
const mapPopupLayer = ref(null);
const popupElement = ref();
const headerAlign = 'center'; // 设置表头的对齐方式为居中
const getMap = (map) => {
  mapInstance.value = map.value;
  console.log('jjj', mapInstance.value);
  //   假数据
  createVectorLayer();
  createPopUpLayer();
  addRandomPoints();
};
const mapClick = (event) => {
  const features = mapInstance.value.getFeaturesAtPixel(event.pixel);
  if (features.length > 0) {
    const selectedFeature = features[0];
    // 获取要素的坐标信息
    popupElement.value.style.display = 'block';
    const coordinate = selectedFeature.getGeometry().getCoordinates();
    mapPopupLayer.value.setPosition(coordinate);
    // 获取地图容器的尺寸和点击事件的位置
    const mapContainerRect = mapInstance.value.getTarget().getBoundingClientRect();
    const clickX = event.pixel[0];
    const clickY = event.pixel[1];

    // 获取弹出框的尺寸
    const popupWidth = popupElement.value.offsetWidth;
    const popupHeight = popupElement.value.offsetHeight;

    // 默认的 positioning
    let positioning = 'top-left';

    // 判断弹出框是否超出地图容器的各个边界
    if (clickX + popupWidth > mapContainerRect.width) {
      positioning = 'top-right';
    } else if (clickX - popupWidth < 0) {
      positioning = 'top-left';
    }

    if (clickY + popupHeight > mapContainerRect.height) {
      positioning = positioning.replace('top', 'bottom');
    } else if (clickY - popupHeight < 0) {
      positioning = positioning.replace('bottom', 'top');
    }

    // 设置弹出框的位置和定位方式
    mapPopupLayer.value.setPositioning(positioning);

    // 设置弹出框的位置
    mapPopupLayer.value.setPosition(coordinate);
    // 在这里添加处理点击事件的逻辑
    console.log('点击了要素：', selectedFeature.getProperties());
  } else {
    mapPopupLayer.value.setPosition(undefined);
  }
};
const createVectorLayer = () => {
  vectorLayer.value = new VectorLayer({
    source: new VectorSource(),
    zIndex: 2,
  });
  mapInstance.value.addLayer(vectorLayer.value);
};
const addRandomPoints = () => {
  if (!vectorLayer.value) {
    return;
  }
  const provinces = [
    { lonMin: 97.35, lonMax: 108.16, latMin: 26.0, latMax: 34.91 }, // 四川
  ];

  for (let i = 0; i < 1; i++) {
    const province = provinces[Math.floor(Math.random() * provinces.length)];
    const lon = Math.random() * (province.lonMax - province.lonMin) + province.lonMin;
    const lat = Math.random() * (province.latMax - province.latMin) + province.latMin;

    const point = new Feature({
      // geometry: new Point([lon, lat]),
      geometry: new Point([116.403218, 39.92372]),
    });

    const style = new Style({
      image: new Icon({
        src: yellowEllipse,
        scale: 1,
      }),
    });

    point.setStyle(style);
    vectorLayer.value.getSource().addFeature(point);
  }
};
const createPopUpLayer = () => {
  let popup = document.getElementById('map-popup');
  mapPopupLayer.value = new Overlay({
    element: popup,
    positioning: 'top-left',
  });
  mapInstance.value.addOverlay(mapPopupLayer.value);
};

// end
</script>

<style scoped lang="scss">
.app-main {
  display: flex;
  justify-content: space-between;

  .app-left {
    margin-left: 30px;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: calc(100% - 40px);
    width: 450px;
    &-total {
      width: 450px;
      height: 80px;
      background-size: 100% 100%;
      background-image: url('../../assets/images/losses/title.png');
      font-size: 22px;
      color: #46ffff;
      line-height: 80px;
      padding-left: 120px;
      box-sizing: border-box;
      display: flex;
      position: absolute;
      top: 100px;
      span {
        font-family: DS-Digital;
        font-weight: 600;
        font-size: 36px;
        padding-top: 22px;
        display: flex;
        margin-left: 8px;
        i {
          display: block;
          width: 26px;
          height: 36px;
          background: #004c86;
          margin: 0 2px;
          font-style: normal;
          line-height: 36px;
          text-align: center;
        }
      }
    }

    &-item {
      width: 450px;
      background: linear-gradient(
        135deg,
        rgba(0, 3, 45, 0.85) 0%,
        rgba(0, 12, 88, 0.85) 50%,
        rgba(0, 3, 45, 0.85) 100%
      );
      position: relative;
      position: absolute;
      &::after {
        content: '';
        display: block;
        width: 450px;
        height: 50px;
        background-size: 100% 100%;
        background-image: url('../../assets/images/common/bg_bottom.png') !important;
        position: absolute;
        bottom: -10px;
      }
      &:nth-child(2) {
        height: 220px;
        top: 200px;
        z-index: 10;
      }
      &:nth-child(3) {
        height: 300px;
        top: 430px;
      }
      &:nth-child(4) {
        height: 296px;
        top: 736px;
      }
    }
  }

  .app-map {
    width: 960px;
    height: 100%;
    overflow: hidden; //test
    position: relative;
  }
  .app-right {
    margin-right: 30px;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: calc(100% - 40px);
    width: 450px;

    &-item {
      width: 450px;
      height: 460px;
      background: linear-gradient(
        135deg,
        rgba(0, 3, 45, 0.85) 0%,
        rgba(0, 12, 88, 0.85) 50%,
        rgba(0, 3, 45, 0.85) 100%
      );
      position: relative;
      &::after {
        content: '';
        display: block;
        width: 450px;
        height: 50px;
        background-size: 100% 100%;
        background-image: url('../../assets/images/common/bg_bottom.png') !important;
        position: absolute;
        bottom: -10px;
      }
      &:nth-child(2) {
        height: 455px;
      }
    }
  }
}
/* start */
.popup-bg {
  width: 400px;
  height: 275px;
  background: linear-gradient(NaNdeg, rgba(5, 12, 53, 0.8) 0%, rgba(4, 16, 89, 0.8) 55%, rgba(6, 40, 150, 0.8) 100%);
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 2px solid #0190f9;
  display: flex;
  padding: 17px 15px;
  flex-direction: column;
  .popup-head {
    justify-content: center;

    display: flex;
    flex-direction: row;
    font-size: 18px;
    font-family: Source Han Sans CN-Bold, Source Han Sans CN;
    font-weight: bold;
    color: #ffffff;
    line-height: 23px;
    .popup-head-title {
      margin-left: auto;
    }
    .popup-head-x {
      margin-left: auto;
      width: 12px;
      height: 12px;
      color: #e6f4fd;
      opacity: 1;
    }
  }
  .popup-content {
    height: 100%;
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    font-size: 16px;
    font-family: Source Han Sans CN-Medium, Source Han Sans CN;
    font-weight: 500;
    color: #e6f4fd;
    line-height: 23px;
    .popup-content-title {
      background: #050426;
      border-radius: 0px 0px 0px 0px;
      opacity: 0.5;
      padding: 10px 34px;
      justify-content: space-between;
      display: flex;
      flex-direction: row;
    }
    .popup-table {
      width: 100%;
      height: 100%;
      margin-right: 20px;
      .centered-cell {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
      }
      .el-table {
        width: 90%;
        max-height: 150px;
        overflow-x: hidden;
        overflow-y: auto;
        z-index: 99;
        position: absolute;
        /* background: linear-gradient(135deg, #00032d 0%, #000c58 50%, #00032d 100%); */
        /* 背景透明 */
        background: rgba(0, 0, 0, 0);
        :deep(tr:hover > td) {
          background: rgba(71, 90, 219, 0.05) !important;
        }
        // top: 70px;
        // background-color: #00032D;
        /* background: linear-gradient(135deg, #00032d 0%, #000c58 50%, #00032d 100%); */
        :deep(.el-table__header) {
          background: rgba(5, 4, 38, 0.4);
          tr {
            background-color: transparent !important;
          }
          th.el-table__cell {
            background-color: transparent !important;
          }
          th.el-table__cell.is-leaf {
            border-bottom: none;
          }
        }

        :deep(.el-table__row) {
          background: rgba(5, 4, 38, 0.4);
        }
        :deep(.el-table__inner-wrapper)::before {
          display: none;
        }
      }
    }
  }
}

/* end */
</style>
